#include "PclRegionGrowingModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.region_growing_model.json";
}

PclRegionGrowingModel::PclRegionGrowingModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/region_growing_segmentation.html");
}

PclRegionGrowingModel& PclRegionGrowingModel::get()
{
    static PclRegionGrowingModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void PclRegionGrowingModel::fromJson(const QJsonObject& json, QStringList& errorList)
{
    BasePclModel::fromJson(json, errorList);

    this->setPcdFile(json["pcdFile"].toString());
    this->kSearch_ = json["kSearch"].toInt();
    this->minClusterSize_ = json["minClusterSize"].toInt();
    this->maxClusterSize_ = json["maxClusterSize"].toInt();
    this->numberOfNeighbours_ = json["numberOfNeighbours"].toInt();
    this->smoothnessThreshold_ = json["smoothnessThreshold"].toDouble();
    this->curvatureThreshold_ = json["curvatureThreshold"].toDouble();
}

QJsonObject PclRegionGrowingModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();
    json.insert("pcdFile", QJsonValue(this->pcdFile_));
    json.insert("kSearch", QJsonValue(this->kSearch_));
    json.insert("minClusterSize", QJsonValue(this->minClusterSize_));
    json.insert("maxClusterSize", QJsonValue(this->maxClusterSize_));
    json.insert("numberOfNeighbours", QJsonValue(this->numberOfNeighbours_));
    json.insert("smoothnessThreshold", QJsonValue(this->smoothnessThreshold_));
    json.insert("curvatureThreshold", QJsonValue(this->curvatureThreshold_));

    return json;
}
